/**
 * ZIT存储工具类(依赖‘cordova.file’和‘cordova.console’插件)
 */
var ZStore = {};

window.requestFileSystem = (window.requestFileSystem || window.webkitRequestFileSystem);

var QUOTA = 1024 * 1024 /* 1MB */;

/**
 * 操作失败
 */
ZStore.onFailure = function(e) {
	console.log('文件操作错误-Name:' + e.name + ", Message:" + e.message);
	return e.message;
};

/**
 * 判断是否PhoneGap环境
 */
var isCordova = function() {
	return (window.cordova);
}

/**
 * 获取数据
 */
ZStore.getItem = function(key, handler) {
	if(!isCordova()) {
		var value = window.localStorage.getItem(key);
		if(handler) {
			handler({
				success: true,
				value: value
			});
		}
		console.log("ZStore本地获取数据-KEY:" + key + ", Value:" + value);
		return;
	}

	var fail = function(evt) {
		var msg = ZStore.onFailure(evt);
		if(handler) {
			handler({
				success: false,
				errMsg: msg
			});
		}
	};

	if(!window.requestFileSystem) {
		handler({
			success: false,
			errMsg: 'ZStore读文件-不支持文件系统！'
		});
		console.log("ZStore获取文件数据-不支持文件系统-KEY:" + key);
		return;
	}

	if(navigator.webkitPersistentStorage) {
		navigator.webkitPersistentStorage.requestQuota(QUOTA, function(grantedBytes) {
			window.requestFileSystem(PERSISTENT, grantedBytes, function(fileSystem) {
				ZStore.getItemInternel(fileSystem, key, fail, handler);
			}, fail);
		}, fail);
	} else {
		window.requestFileSystem(PERSISTENT, QUOTA, function(fileSystem) {
			ZStore.getItemInternel(fileSystem, key, fail, handler);
		}, fail);
	}
};

ZStore.getItemInternel = function(fileSystem, key, fail, handler) {
	fileSystem.root.getFile("zit/" + key, {
		create: true,
		exclusive: false
	}, function(fileEntry) {
		fileEntry.file(function(file) {
			var reader = new FileReader();
			reader.onloadend = function(evt) {
				// 回调处理文件
				var value = evt.target.result;
				console.log("读取文件(" + key + ": " + value + ")成功！");
				handler({
					success: true,
					value: value
				});
			};
			reader.readAsText(file);
		}, fail);
	}, fail);
};

/**
 * 存储数据
 */
ZStore.setItem = function(key, value, handler) {
	if(!isCordova()) {
		window.localStorage.setItem(key, value);
		if(handler) {
			handler({
				success: true
			});
		}
		console.log("ZStore本地存储数据-KEY:" + key + ", Value:" + value);
		return;
	}

	var fail = function(evt) {
		var msg = ZStore.onFailure(evt);
		if(handler) {
			handler({
				success: false,
				errMsg: msg
			});
		}
	};

	if(!window.requestFileSystem) {
		handler({
			success: false,
			errMsg: 'ZStore写文件-不支持文件系统！'
		});
		console.log("ZStore存储文件数据-不支持文件系统-KEY:" + key);
		return;
	}

	if(navigator.webkitPersistentStorage) {
		navigator.webkitPersistentStorage.requestQuota(QUOTA, function(grantedBytes) {
			window.requestFileSystem(PERSISTENT, grantedBytes, function(fileSystem) {
				ZStore.setItemInternel(fileSystem, key, value, fail, handler);
			}, fail);
		}, fail);
	} else {
		window.requestFileSystem(PERSISTENT, QUOTA, function(fileSystem) {
			ZStore.setItemInternel(fileSystem, key, value, fail, handler);
		}, fail);
	}
};

ZStore.setItemInternel = function(fileSystem, key, value, fail, handler) {
	fileSystem.root.getDirectory("zit", {
		create: true,
		exclusive: false
	}, function(dirEntry) {
		dirEntry.getFile(key, {
			create: true,
			exclusive: false
		}, function(fileEntry) {
			fileEntry.createWriter(function(writer) {
				// 存储数据内容
				console.log("开始写入文件(" + key + ": " + value + ")...");
				// var blob = new Blob([value]); /* { type: 'text/plain;charset=UTF-8' } */
				// writer.write(blob);
				writer.write(value);

				// 写入文件回调
				console.log("写入文件(" + key + ": " + value + ")成功！");
				handler({
					success: true
				});
			}, fail);
		}, fail);
	}, fail);
};

/**
 * 删除数据
 */
ZStore.removeItem = function(key, handler) {
	if(!isCordova()) {
		window.localStorage.removeItem(key);
		if(handler) {
			handler({
				success: true
			});
		}
		console.log("ZStore本地删除数据-KEY:" + key);
		return;
	}

	var fail = function(evt) {
		var msg = ZStore.onFailure(evt);
		if(handler) {
			handler({
				success: false,
				errMsg: msg
			});
		}
	};

	if(!window.requestFileSystem) {
		handler({
			success: false,
			errMsg: 'ZStore删除文件-不支持文件系统！'
		});
		console.log("ZStore删除文件数据-不支持文件系统-KEY:" + key);
		return;
	}

	if(navigator.webkitPersistentStorage) {
		navigator.webkitPersistentStorage.requestQuota(QUOTA, function(grantedBytes) {
			window.requestFileSystem(PERSISTENT, grantedBytes, function(fileSystem) {
				ZStore.removeItemInternel(fileSystem, key, fail, handler);
			}, fail);
		}, fail);
	} else {
		window.requestFileSystem(PERSISTENT, QUOTA, function(fileSystem) {
			ZStore.removeItemInternel(fileSystem, key, fail, handler);
		}, fail);
	}
};

ZStore.removeItemInternel = function(fileSystem, key, fail, handler) {
	fileSystem.root.getDirectory("zit", {
		create: false,
		exclusive: false
	}, function(dirEntry) {
		dirEntry.getFile(key, {
			create: false,
			exclusive: false
		}, function(fileEntry) {
			fileEntry.remove(function() {
				// 删除文件回调
				console.log("删除文件(" + key + ")成功！");
				handler({
					success: true
				});
			}, fail);
		}, fail);
	}, fail);
};
